﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo
{
    public partial class InternetTest : Form
    {
        public InternetTest()
        {
            InitializeComponent();
        }

        private void InternetTest_Load(object sender, EventArgs e)
        {

        }

        public string parseHtml(String pageHtml)
        {
            string ip = "";
            Match m = Regex.Match(pageHtml, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
            if (m.Success)
            {
                ip = m.Value;
            }
            else
            {
                ip = "解释不到IP";
            }
            return ip;
        }

        public  string HttpGet(string url, string encoding)
        {
            string result = string.Empty;
            try
            {
                Encoding encode = System.Text.Encoding.GetEncoding(encoding); //设置编码
                                                                              //获取HttpWebRequest对象
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Method = "GET";
                wbRequest.Timeout = 1000;
                wbRequest.UseDefaultCredentials = true;
                //设置用户代理
                wbRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36";
                //获取返回流
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    //读取返回流
                    using (StreamReader sReader = new StreamReader(responseStream, encode))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                txtEcho.Text = ex.Message;
            }
            return result;
        }
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(int Description, int ReservedValue);

       
        /// <summary>
        /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败 
        /// </summary>
        /// <returns></returns>
        public static bool IsConnectInternet()
        {
            int Description = 0;
            return InternetGetConnectedState(Description, 0);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string text = HttpGet(txtDns.Text, "utf-8");
            txtEcho.Text = text;
            string ip = parseHtml(text);
            lbExternalIP.Text = ip;
        }

        private void btnConnState_Click(object sender, EventArgs e)
        {
            lbConnStatus.Text = IsConnectInternet() ? "Internet Connected" : "Internet NOT Connected";
        }
    }
}
